#include "sliderwithbtns.h"

SliderWithBtns::SliderWithBtns(const QString str, QWidget *parent) : QWidget(parent)
{
    QHBoxLayout *hbox = new QHBoxLayout(this);

    text = new QLabel(str + ":");
    val = new QLabel(QString::number(0));
    btn_minus = new QPushButton("-", this);
    slider = new QSlider(Qt::Horizontal, this);
    btn_plus = new QPushButton("+", this);

    text->setMinimumWidth(30);
    btn_minus->setFixedWidth(25);
    slider->setTickPosition(QSlider::TicksBelow);
    btn_plus->setFixedWidth(25);

    hbox->addWidget(text);
    hbox->addWidget(val);
    hbox->addWidget(btn_minus);
    hbox->addWidget(slider);
    hbox->addWidget(btn_plus);

    setLayout(hbox);

    //减小操作
    connect(btn_minus, &QPushButton::clicked, this, [=]() {
        slider->setValue(slider->value() - 10);
    });
    //增大操作
    connect(btn_plus, &QPushButton::clicked, this, [=]() {
        slider->setValue(slider->value() + 10);
    });
    //slider滑动
    connect(slider, &QSlider::valueChanged, this, &SliderWithBtns::valueChanged);
    connect(slider, &QSlider::valueChanged, this, [=]() {
       val->setText(QString::number(slider->value()));
    });

}

int SliderWithBtns::value() const
{
    return slider->value();
}

void SliderWithBtns::setRange(int min, int max)
{
    slider->setRange(min, max);
}

QPushButton *SliderWithBtns::btnMinus() const
{
    return btn_minus;
}

QPushButton *SliderWithBtns::btnPlus() const
{
    return btn_plus;
}
